/**
 * Copyright (c) 2017-2024 Nop Platform. All rights reserved.
 * Author: canonical_entropy@163.com
 * Blog:   https://www.zhihu.com/people/canonical-entropy
 * Gitee:  https://gitee.com/canonical-entropy/nop-entropy
 * Github: https://github.com/entropy-cloud/nop-entropy
 */
package com.gitee.wsl.text

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull


class TestStringTrie {
    @Test
    fun testAdd() {
        val trie: StringTrie<String> = StringTrie<String>()
        trie.add("abcd", "ABCD")

        trie.add("ab", "AB")
        
        trie.add("abce", "abce")
        trie.put("abcd", "ABCd")
        
        trie.add("abc", "abc")
        
        trie.add("a", "a")
        trie.add("ef", "ef")
        trie.add("cde", "cde")
        trie.add("cdkf", "cdkf")
        
        val l1 = trie.find("ab")
        val l2 = trie.find("abc")
        val l3 = trie.find("abcd")
        val l4 = trie.find("abce")
        val l5 = trie.find("abcde")
        assertEquals("AB", l1.toString())
        assertEquals("abc", l2.toString())
        assertEquals("ABCd", l3.toString())
        assertEquals("abce", l4.toString())
        assertNull(l5)

        assertEquals(4, trie.findNode("abcde", true)?.keyLength)
    }

    @Test
    fun testFindWithPrefix() {
        val trie: StringTrie<String> = StringTrie()
        trie.add("/p/_info/", "ABCD")
        trie.add("/_bbs/", "ABCD")

        trie.add("/_info/", "abce")
        
        trie.add("/_info/ch1/", "abct")
        
        trie.add("/_info/ch2/", "abcv")
        val sb = StringBuilder()
        trie.dump(sb)
        println(sb)
        assertEquals("abct", trie.findWithPrefix("/_info/ch1/abc").toString())
        assertEquals("abcv", trie.find("/_info/ch2/").toString())
    }
}